package huoye.shops.common;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import huoye.shops.R;
import huoye.shops.base.APIUrls;
import huoye.shops.huoye.operation.model.AddImage;

/**
 * Created by user on 2017/10/16.
 */

public class AddPictureAdapter extends RecyclerViewAdapter<AddPictureAdapter.ViewHolder, AddImage> {


    public AddPictureAdapter(Context context) {
        super(context, R.layout.item_add_pic);

    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    private int maxCount;
    @Override
    protected ViewHolder initViewHolder(View view, int i) {
        return new ViewHolder(view);
    }

    @Override
    protected void bindView(final ViewHolder viewHolder, final AddImage addImage, final int positon) {
        showImg(viewHolder, addImage, positon);


        viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData().remove(positon);
                notifyDataSetChanged();
            }

        });

    }

    private void showImg(ViewHolder viewHolder, AddImage addImage, int positon) {
        if (positon < getListCount()) {
            viewHolder.ivDelete.setVisibility(View.VISIBLE);
            if (addImage.isLocal()) {
                Glide.with(getContext())
                        .load(new File(addImage.getPath()))
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .placeholder(R.mipmap.ic_wait_logo)
                        .error(R.mipmap.ic_error)
                        .into(viewHolder.iv);
            } else {
                Glide.with(getContext())
                        .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+addImage.getImageUrl())
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .placeholder(R.mipmap.ic_wait_logo)
                        .error(R.mipmap.ic_error)
                        .into(viewHolder.iv);

            }

        } else {
            viewHolder.iv.setImageResource(R.mipmap.ic_add_image);//最后一个显示加号图片
            viewHolder.ivDelete.setVisibility(View.GONE);
        }


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            return new ViewHolder((View)this.getHeaderViews().get(0));
        } else if (viewType == -2) {
            return new ViewHolder((View)this.getHeaderViews().get(0));
        } else {
            ViewHolder v = this.onCreateViewDataHolder(this.mLayoutInflater, parent, viewType);
            if (v != null && v.itemView != null) {
                v.itemView.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        int position = (Integer)view.getTag();
                        if (onRecyclerViewItemClickListener != null&&position<getItemCount()-1) {
                            onRecyclerViewItemClickListener.onItemClick(getData().get(position), position, view);
                        }else if (onRecyclerViewItemClickListener != null&&position==getItemCount()-1){
                            onRecyclerViewItemClickListener.onItemClick(null, position, view);
                        }

                    }
                });
            }
            return v;
        }
    }


    public ViewHolder onCreateViewDataHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        View view = this.createHolderView(inflater, parent, viewType);
        return this.initViewHolder(view, viewType);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (this.getItemViewType(position) != -1) {
            if (this.getItemViewType(position) == -2) {
                holder.itemView.setVisibility(View.VISIBLE);
            } else {
                int pos = this.getRealPosition(holder);
                if (pos != this.getListCount()) {
                    AddImage data = getData().get(pos);
                    if (data != null) {
                        holder.itemView.setTag(pos);
                        this.bindView(holder, data, pos);
                    }
                }else {
                    holder.itemView.setTag(pos);
                    this.bindView(holder, null, pos);
                }
            }
        }
    }

 /*   int i = 1;
    private void showImg(final ViewHolder viewHolder, final AddImage addImage, final int positon) {
        if (getListCount() == maxCount ) {
            //6张图
            if (positon == maxCount) {
                viewHolder.ivDelete.setVisibility(View.GONE);
                viewHolder.iv.setVisibility(View.INVISIBLE);
            } else {
                if (addImage.isLocal()) {
                    Glide.with(getContext())
                            .load(new File(addImage.getPath()))
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.mipmap.ic_wait_logo)
                            .error(R.mipmap.ic_error)
                            .into(viewHolder.iv);
                } else {
                    Glide.with(getContext())
                            .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+addImage.getImageUrl())
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.mipmap.ic_wait_logo)
                            .error(R.mipmap.ic_error)
                            .into(viewHolder.iv);
                }
            }
        } else {
            // 有5张图片。但是 你这里吧最后一张图片换成了 add图标
            if (positon == getItemCount() - i) {
                viewHolder.iv.setVisibility(View.VISIBLE);
                Glide.with(getContext())
                        .load(R.mipmap.ic_add_image)
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .placeholder(R.mipmap.ic_wait_logo)
                        .error(R.mipmap.ic_error)
                        .into(viewHolder.iv);
                viewHolder.ivDelete.setVisibility(View.GONE);
            } else {
                viewHolder.iv.setVisibility(View.VISIBLE);
                viewHolder.ivDelete.setVisibility(View.VISIBLE);
                if (addImage.isLocal()) {
                    Glide.with(getContext())
                            .load(new File(addImage.getPath()))
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.mipmap.ic_wait_logo)
                            .error(R.mipmap.ic_error)
                            .into(viewHolder.iv);
                } else {
                    Glide.with(getContext())
                            .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+addImage.getImageUrl())
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.mipmap.ic_wait_logo)
                            .error(R.mipmap.ic_error)
                            .into(viewHolder.iv);

                }


            }
        }



    }*/


    @Override
    public void add(AddImage data) {
        getData().add(data);
    }

    public static class ViewHolder extends RecyclerViewAdapter.ViewHolder {

        @BindView(R.id.item_add_pic_iv)
        ImageView iv;
        @BindView(R.id.item_delete_pic_iv)
        ImageView ivDelete;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public int getItemCount() {
        if (getListCount() >= maxCount){
            return maxCount;
        }else {
            return getData().size() +1  ;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position < this.getHeaderViews().size()) {
            return - 1;
        } else {
            return position >= this.getHeaderViews().size() + this.getListCount() +1? - 2 : this.getDataItemViewType(position - this.getHeaderViews().size());
        }

    }


    protected OnItemClickListener onRecyclerViewItemClickListener;
    public interface OnItemClickListener<T> {
        void onItemClick(T var1, int var2, View var3);
    }
    public void setOnItemClickListener(OnItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }







    /*private void showImg(final ViewHolder viewHolder, final AddImage addImage, final int positon) {
        Log.e("aaaaaaaaaa",getListCount()+"i");
        if (getListCount() == i) {
            Glide.with(getContext())
                    .load(R.mipmap.ic_add_image)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .placeholder(R.mipmap.ic_wait_logo)
                    .error(R.mipmap.ic_error)
                    .into(viewHolder.iv);
            viewHolder.iv.setVisibility(View.VISIBLE);
            viewHolder.ivDelete.setVisibility(View.GONE);
        } else if (getListCount() == maxCount + i) {
            if (positon == maxCount) {
                viewHolder.ivDelete.setVisibility(View.GONE);
                viewHolder.iv.setVisibility(View.INVISIBLE);
            } else {
                viewHolder.ivDelete.setVisibility(View.VISIBLE);
                viewHolder.iv.setVisibility(View.VISIBLE);
                if (addImage.isLocal()) {
                    Glide.with(getContext())
                            .load(new File(addImage.getPath()))
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.mipmap.ic_wait_logo)
                            .error(R.mipmap.ic_error)
                            .into(viewHolder.iv);
                } else {
                    Glide.with(getContext())
                            .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+addImage.getImageUrl())
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.mipmap.ic_wait_logo)
                            .error(R.mipmap.ic_error)
                            .into(viewHolder.iv);

                }
            }
        } else {
            if (positon == getListCount() - i) {  //当我的positon  == 5  的时候进来这里 被改变了
                viewHolder.iv.setVisibility(View.VISIBLE);
                Glide.with(getContext())
                        .load(R.mipmap.ic_add_image)
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .placeholder(R.mipmap.ic_wait_logo)
                        .error(R.mipmap.ic_error)
                        .into(viewHolder.iv);
                viewHolder.ivDelete.setVisibility(View.GONE);
            } else {
                viewHolder.iv.setVisibility(View.VISIBLE);
                viewHolder.ivDelete.setVisibility(View.VISIBLE);
                if (addImage.isLocal()) {
                    Glide.with(getContext())
                            .load(new File(addImage.getPath()))
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.mipmap.ic_wait_logo)
                            .error(R.mipmap.ic_error)
                            .into(viewHolder.iv);
                } else {
                    Glide.with(getContext())
                            .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+addImage.getImageUrl())
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.mipmap.ic_wait_logo)
                            .error(R.mipmap.ic_error)
                            .into(viewHolder.iv);
                }

            }
        }
    }
*/


}
